package com.fastbee.json;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.fastbee.common.annotation.SysProtocol;
import com.fastbee.common.constant.FastBeeConstant;
import com.fastbee.common.core.mq.DeviceReport;
import com.fastbee.common.core.mq.message.DeviceData;
import com.fastbee.common.core.thingsModel.ThingsModelSimpleItem;
import com.fastbee.common.core.thingsModel.ThingsModelValuesInput;
import com.fastbee.common.exception.ServiceException;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.iot.model.ThingsModels.ValueItem;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author gsb
 * @date 2022/10/10 16:55
 */
@Slf4j
@Component
@SysProtocol(name = "JSONArray解析协议",protocolCode = FastBeeConstant.PROTOCOL.JsonArray,description = "系统内置JSONArray解析协议")
public class JsonProtocolService   {


    public DeviceReport decode(DeviceData deviceData, String clientId) {
        try {
            DeviceReport reportMessage = new DeviceReport();
            // bytep[] 转String
            String data = new String(deviceData.getData(),StandardCharsets.UTF_8);
            List<ThingsModelSimpleItem> values = JSON.parseArray(data, ThingsModelSimpleItem.class);

            List<ThingsModelSimpleItem> result = convertToThingsModelItems(data);
            ThingsModelValuesInput valuesInput = new ThingsModelValuesInput();
            valuesInput.setThingsModelValueRemarkItem(result);
            reportMessage.setValuesInput(valuesInput);
            reportMessage.setClientId(clientId);
            reportMessage.setSerialNumber(clientId);
            return reportMessage;
        }catch (Exception e){
            throw new ServiceException("数据解析异常"+e.getMessage());
        }
    }

    public  List<ThingsModelSimpleItem> convertToThingsModelItems(String jsonStr) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        ObjectNode rootNode = (ObjectNode) objectMapper.readTree(jsonStr);

        List<ThingsModelSimpleItem> items = new ArrayList<>();

        // 添加简单属性
        addSimpleProperty(items, "monitorTime", rootNode.get("monitorTime").asText());
        addSimpleProperty(items, "deviceCode", rootNode.get("deviceCode").asText());
        addSimpleProperty(items, "deviceName", rootNode.get("deviceName").asText());
        addSimpleProperty(items, "deviceModel", rootNode.get("deviceModel").asText());
        addSimpleProperty(items, "deviceNorm", rootNode.get("deviceNorm").asText());
        addSimpleProperty(items, "gatewayId", rootNode.get("gatewayId").asText());

        // 处理properties对象
        ObjectNode propertiesNode = (ObjectNode) rootNode.get("properties");
        if (propertiesNode != null) {
            // 将properties对象转为JSON字符串
            String propertiesJson = propertiesNode.toString();
            addSimpleProperty(items, "properties", propertiesJson);
        }
        return items;
    }

    private static void addSimpleProperty(List<ThingsModelSimpleItem> items, String id, String value) {
        ThingsModelSimpleItem item = new ThingsModelSimpleItem();
        item.setId(id);
        item.setValue(value);
        items.add(item);
    }



    public byte[] encode(DeviceData message, String clientId) {
        try {
            JSONObject body = (JSONObject) message.getDownMessage().getBody();
            ValueItem valueItem = new ValueItem();
            for (Map.Entry<String, Object> entry : body.entrySet()) {
                valueItem.setId(entry.getKey());
                valueItem.setValue(entry.getValue()+"");
                valueItem.setRemark("");
            }
            String msg = "[" + JSONObject.toJSONString(valueItem) +"]";
            return msg.getBytes(StandardCharsets.UTF_8);
        }catch (Exception e){
            log.error("=>指令编码异常,device={},data={}",message.getSerialNumber(),
                    message.getDownMessage().getBody());
            return null;
        }
    }
}
